package morenoapp.components.common;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashSet;
import java.util.Set;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JLabel;

/**
 *  Label which behaves like hyperlink.
 *
 * @author dmitry.mamonov
 */
public class LinkLabel extends JLabel {
    Set<ActionListener> listeners = new HashSet<ActionListener>();
    boolean highlited = false;
    boolean selected = false;
    boolean highlightBackground = false;
    Action action = null;
    PropertyChangeListener actionPropertyChangeListener = new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt != null && "enabled".equals(evt.getPropertyName()) == true) {
                Boolean newValue = (Boolean) evt.getNewValue();
                if (newValue != isEnabled()) {
                    setEnabled(newValue);
                }
            }
        }

    };
    public LinkLabel() {
        super();
        setForeground(new Color(0x0000CC));
        setBackground(new Color(0xC3D9FF));
        setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                ActionEvent event = new ActionEvent(this, 0, "clicked");
                for (ActionListener lst : listeners) {
                    lst.actionPerformed(event);
                }
                if (action != null) {
                    action.actionPerformed(event);
                }
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                highlited = true;
                repaint();
            }

            @Override
            public void mouseExited(MouseEvent e) {
                highlited = false;
                repaint();
            }

        });

    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        boolean oldSelected = this.selected;
        this.selected = selected;
        if (oldSelected != selected) {
            repaint();
        }
    }
    public boolean isHighlightBackground() {
        return highlightBackground;
    }

    public void setHighlightBackground(boolean highlightBAckground) {
        this.highlightBackground = highlightBAckground;
    }

    public Action getAction() {
        return action;
    }

    public void setAction(Action _action) {
        if (this.action != null) {
            this.action.removePropertyChangeListener(actionPropertyChangeListener);
        }
        this.action = _action;
        action.addPropertyChangeListener(actionPropertyChangeListener);
        setText((String) action.getValue(Action.NAME));
        setIcon((Icon) action.getValue(Action.SMALL_ICON));
        setToolTipText((String) action.getValue(Action.SHORT_DESCRIPTION));
    }

    public void addActionListener(ActionListener lst) {
        listeners.add(lst);
    }

    public void removeActionListener(ActionListener lst) {
        listeners.remove(lst);
    }

    @Override
    public void paint(Graphics g) {
        int width = getWidth();
        int height = getHeight();
        if (highlightBackground == true) {
            if ((highlited && isEnabled()) || selected) {
                Color selectedBackground = getBackground();
                if (highlited && !selected) {
                    int addToColor = 15;
                    selectedBackground = new Color(
                            Math.min(255, selectedBackground.getRed() + addToColor),
                            Math.min(255, selectedBackground.getGreen() + addToColor),
                            Math.min(255, selectedBackground.getBlue() + addToColor));
                }
                g.setColor(selectedBackground);
                g.fillRect(0, 0, width, height);
            }
        }
        super.paint(g);
        if (highlited && isEnabled()) {
            int baseline = getBaseline(width, height) + 1;
            g.setColor(getForeground());
            g.drawLine(0, baseline, width, baseline);
        }
    }

}
